package com.example.day2.zhibo;

import android.content.Intent;
import android.view.View;

import androidx.lifecycle.Observer;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import com.blankj.utilcode.util.ToastUtils;
import com.example.day2.R;

import com.example.day2.databinding.ActivityZhibaoBinding;

import com.example.day2.entity.StartParam;
import com.example.day2.entity.StartResult;
import com.example.day2.mvvm.base.BaseActivity;


public class StartActivity extends BaseActivity<ActivityZhibaoBinding,StartViewModel> {
    public LocationClient mLocationClient = null;
    private  String locationDescribe;
    private MyLocationListener myListener = new MyLocationListener();
    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location){
            //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
            //以下只列举部分获取位置描述信息相关的结果
            //更多结果信息获取说明，请参照类参考中BDLocation类中的说明

            System.out.println(location.getAddrStr());
            ToastUtils.showLong(location.getCity());
            System.out.println(location.getProvince());
             locationDescribe = location.getLocationDescribe();//获取位置描述信息
             binding.address.setText(locationDescribe);

            System.out.println(locationDescribe+"----======");

             mLocationClient.stop();
        }
    }
    @Override
    public void initView() {

        LocationClientOption option = new LocationClientOption();
        option.setIsNeedLocationDescribe(true);
        mLocationClient = new LocationClient(getApplicationContext());
        //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);
        //注册监听函数
        mLocationClient.setLocOption(option);
        mLocationClient.start();


        binding.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String address = binding.address.getText().toString();
                String caption = binding.caption.getText().toString();
                StartParam startParam = new StartParam();
                startParam.setAddress(address);
                startParam.setCaption(caption);
                viewModel.getStart(startParam);
            }
        });

    }

    @Override
    public void initData() {
        viewModel.successStart.observe(this, new Observer<StartResult>() {
            @Override
            public void onChanged(StartResult startResult) {
                    Intent intent = new Intent(StartActivity.this, ZhiboActivity.class);
                    intent.putExtra("url",startResult.getData().getUrl_push());
                    startActivity(intent);


            }
        });

    }

    @Override
    public Class<StartViewModel> getVIewModel() {
        return StartViewModel.class;
    }

    @Override
    public int bindLayoutId() {
        return R.layout.activity_zhibao;
    }
}
